package xyz.poke.business.tool.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import xyz.poke.business.tool.common.PageResult;
import xyz.poke.business.tool.common.Response;
import xyz.poke.business.tool.constants.ErrorConstants;
import xyz.poke.business.tool.convert.GoodsConvert;
import xyz.poke.business.tool.dto.GoodsDTO;
import xyz.poke.business.tool.entity.Goods;
import xyz.poke.business.tool.param.GoodsParam;
import xyz.poke.business.tool.service.IGoodsService;
import xyz.poke.business.tool.utils.CheckerUtils;
import xyz.poke.business.tool.utils.ResponseUtils;
import xyz.poke.business.tool.vo.GoodsVo;

/**
 * <p>
 * 商品基本信息表 前端控制器
 * </p>
 *
 * @author 迫壳
 * @since 2024-02-22
 */
@Controller
@RequestMapping("/goods")
@ResponseBody
public class GoodsController {

    @Autowired
    private IGoodsService goodsService;

    @PostMapping("paging")
    public Response<PageResult<GoodsVo>> paging(@RequestBody GoodsParam param) {
        PageResult<GoodsDTO> paging = goodsService.paging(param);
        return ResponseUtils.ok(GoodsConvert.INSTANCE.toPaging(paging));
    }

    @PostMapping()
    public Response<Boolean> add(@RequestBody GoodsParam param) {
        Goods goods = GoodsConvert.INSTANCE.toEntity(param);
        CheckerUtils.notNull(param.getCategoryId(), ErrorConstants.CATEGORY_NOT_NULL);
        CheckerUtils.notNull(param.getName(), ErrorConstants.NAME_NOT_NULL);
        return ResponseUtils.ok(goodsService.save(goods));
    }

    @PutMapping()
    public Response<Boolean> update(@RequestBody GoodsParam param) {
        CheckerUtils.notNull(param.getId(), ErrorConstants.ID_NOT_NULL);
        Goods goods = GoodsConvert.INSTANCE.toEntity(param);
        return ResponseUtils.ok(goodsService.updateById(goods));
    }

    @DeleteMapping("/{id}")
    public Response<Boolean> delete(@PathVariable(value = "id") Integer id) {
        CheckerUtils.notNull(id, ErrorConstants.ID_NOT_NULL);
        Goods goods = new Goods();
        goods.setId(id);
        goods.setIsDel(true);
        return ResponseUtils.ok(goodsService.updateById(goods));
    }

}
